<template>
  <div>
    <Header v-if="shows === 'list'">
      <div>
        <el-button size="small" type="primary" @click="add">新建权限</el-button>
      </div>
    </Header>
    <Main
      v-if="shows === 'list'"
      :data="listData"
      :footer-lock="totals > 10"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        width="30"
        :reserve-selection="true"
        type="selection"
      ></el-table-column>
      <el-table-column
        label="门店权限名称"
        align="center"
        property="type_name"
      ></el-table-column>

      <el-table-column
        label="门店权限数量"
        align="center"
        property="name"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="200"
        class-name="tableoperation"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.$index === 0"
            size="mini"
            type="text"
            @click="edit(scope.row)"
          >
            查看
          </el-button>
          <el-button
            v-if="scope.$index !== 0"
            size="mini"
            type="text"
            @click="edit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-if="scope.$index !== 0"
            size="mini"
            type="text"
            @click="del(scope.row.authority_id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>

      <Footer :total="totals">
        <el-pagination
          :page-size="10"
          layout="prev, pager, next"
          :total="totals"
          @current-change="changes"
        >
        </el-pagination>
      </Footer>
    </Main>
    <Form v-if="shows === 'add'" :data-msg="dataMsg" @out="out" />
  </div>
</template>
<script>
import { AUTHORITYLIST, DELAUTHORITY } from "@/service/shop";

export default {
  components: {
    Form: () => import("./form")
  },
  data() {
    return {
      allList: [],
      getRowKeys() {
        // return row.id
      },
      listData: [],
      totals: 0,
      pages: 1,
      shows: "list",
      form: {
        name: ""
      },
      dataMsg: {},
      dialogFormVisible: false,
      dialogTitle: "新增标签"
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSelectionChange(v) {
      this.allList = v;
    },
    add() {
      this.shows = "add";
    },
    edit() {
      this.shows = "add";
      this.dataMsg = { name: "mor" };
    },
    out() {
      this.shows = "list";
      this.dataMsg = {};
      this.getList();
    },
    changes(page) {
      this.pages = page;
      this.getList();
    },
    async getList() {
      await AUTHORITYLIST({ page: this.pages, pageshow: 10 }).then(res => {
        let { code, data, msg } = res;
        if (code === 200) {
          this.totals = data.count;
          data.list.forEach(ele => {
            if (ele.soft === 1) {
              ele.soft = true;
            } else {
              ele.soft = false;
            }
          });
          this.listData = data.list;
        } else {
          this.$message.error(msg);
        }
      });
    },
    async del(authorityId) {
      await this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DELAUTHORITY({
            authority_id: authorityId
          }).then(res => {
            let { code, msg } = res;
            if (code === 200) {
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        })
        .catch(() => {
          this.$message.info("已取消!");
        });
    }
  }
};
</script>
